package com.example.teststarter.controller;

import com.example.teststarter.entity.User;
import com.example.teststarter.service.UserService;
import com.zsh.AuthorServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 60055558 项目名称：springboot 类名称：UserController 创建人：60055558
 * 创建时间：2020年7月17日 下午4:09:01
 */

//@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/userController")
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private AuthorServer authorServer;

    //获取自定义stater方法
    @RequestMapping("/author")
    String home() {
        return "发布者：" + authorServer.getAuthor();
    }

    // 查询
    @GetMapping("/getUser")
    @ResponseBody
    public String GetUsers(@RequestParam(value = "id") int id) {
        return userService.Sel(id).toString();
    }

    // 查询
    @RequestMapping("/getUser/{id}")
    @ResponseBody
    public String GetUser(@PathVariable int id) {
        return userService.Sel(id).toString();
    }

    // 更新
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(HttpServletRequest request) {
        String realName = request.getParameter("realName");
        int id = Integer.parseInt(request.getParameter("id"));
        User user = new User();
        user.setRealName(realName);
        user.setId(id);
        if (userService.updateUserById(user)) {
            return "更新成功！";
        } else {
            return "更新失败！";
        }
    }

    // 新增
    @RequestMapping("/insertUser")
    @ResponseBody
    public String insertUser(HttpServletRequest request) {
		/*
		int id = Integer.parseInt(request.getParameter("id"));
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String realName = request.getParameter("realName");
		User user = new User();
		user.setId(id);
		user.setUserName(userName);
		user.setPassWord(passWord);
		user.setRealName(realName);
		*/
        if (userService.insertUser()) {
            return "插入成功！";
        } else {
            return "插入失败！";
        }
    }

    // 删除
    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(int id) {
        if (userService.deleteById(id)) {
            return "删除成功！";
        } else {
            return "删除失败！";
        }
    }

    //获取用户名
    @RequestMapping("/getUserNameByNum")
    public String getUserNameByNum(int num) {
        return userService.getUserNameByNum(num);
    }

    //通过Function获取用户名
    @RequestMapping("/getFunctionUserNameByNum")
    public String getFunctionUserNameByNum(int num) {
        return userService.getFunctionUserNameByNum(num);
    }

}
